/**
 * @file streaming_example.cpp
 * @brief Example demonstrating high-performance streaming gRPC with Unix Domain Socket
 *
 * This example shows how to use the new streaming client features:
 * - UDS (Unix Domain Socket) for ultra-low latency local connections
 * - Streaming gRPC for batch writes
 * - Columnar batch encoding for efficient data transfer
 *
 * To run this example:
 * 1. Start the ingestd server: ./server/ingestd/ingestd
 * 2. Run this example: ./streaming_example
 */

#include "kwdb/stream_client.h"
#include "kwdb/client.h"
#include "kwdb/point.h"
#include <iostream>
#include <chrono>
#include <thread>

using namespace kwdb;

int main() {
    std::cout << "=== Testing Streaming gRPC with UDS ===" << std::endl;

    // Test 1: UDS connection
    {
        std::cout << "\n[Test 1] Testing UDS connection..." << std::endl;
        IngestConfig config;
        config.ingest_endpoint = "local";  // Should auto-detect UDS

        StreamClient client(config);
        Status status = client.StartStream();

        if (status.ok()) {
            std::cout << "✅ UDS connection successful!" << std::endl;

            // Test writing a single point
            Point point;
            point.measurement("test_stream")
                 .add_tag("host", "server1")
                 .add_field("cpu", 45.5)
                 .timestamp_now();

            status = client.WritePoint(point);
            if (status.ok()) {
                std::cout << "✅ Single point write successful!" << std::endl;
            } else {
                std::cout << "❌ Point write failed: " << status.message << std::endl;
            }

            // Finish the stream
            client.Finish();
        } else {
            std::cout << "❌ UDS connection failed: " << status.message << std::endl;
        }
    }

    // Test 2: Batch streaming
    {
        std::cout << "\n[Test 2] Testing batch streaming..." << std::endl;
        IngestConfig config;
        config.ingest_endpoint = "local";

        StreamClient client(config);
        Status status = client.StartStream();

        if (status.ok()) {
            std::vector<Point> batch;
            for (int i = 0; i < 100; i++) {
                Point point("test_batch");
                point.add_tag("host", "server" + std::to_string(i % 3))
                     .add_field("value", i * 1.5)
                     .timestamp_now();
                batch.push_back(point);
            }

            status = client.WriteBatch(batch);
            if (status.ok()) {
                std::cout << "✅ Batch write of 100 points successful!" << std::endl;
            } else {
                std::cout << "❌ Batch write failed: " << status.message << std::endl;
            }

            client.Finish();
        } else {
            std::cout << "❌ Stream start failed: " << status.message << std::endl;
        }
    }

    // Test 3: Columnar batch
    {
        std::cout << "\n[Test 3] Testing columnar batch..." << std::endl;
        IngestConfig config;
        config.ingest_endpoint = "local";

        StreamClient client(config);

        std::vector<int64_t> timestamps;
        std::map<std::string, std::vector<std::string>> tags;
        std::map<std::string, std::vector<double>> fields;

        // Generate columnar data
        for (int i = 0; i < 50; i++) {
            timestamps.push_back(std::chrono::system_clock::now().time_since_epoch().count() + i);
        }

        tags["host"] = std::vector<std::string>(50);
        tags["region"] = std::vector<std::string>(50);
        for (int i = 0; i < 50; i++) {
            tags["host"][i] = "server" + std::to_string(i % 5);
            tags["region"][i] = "us-east-" + std::to_string(i % 2);
        }

        fields["cpu"] = std::vector<double>(50);
        fields["memory"] = std::vector<double>(50);
        for (int i = 0; i < 50; i++) {
            fields["cpu"][i] = 30.0 + (i % 40);
            fields["memory"][i] = 60.0 + (i % 20);
        }

        Status status = client.WriteColumnar("test_columnar", timestamps, tags, fields);
        if (status.ok()) {
            std::cout << "✅ Columnar batch write of 50 rows successful!" << std::endl;
        } else {
            std::cout << "❌ Columnar write failed: " << status.message << std::endl;
        }
    }

    std::cout << "\n=== All streaming tests completed ===" << std::endl;

    return 0;
}